//
//  ViewController.m
//  UICollectionView基本使用
//
//  Created by 泛在吕俊衡 on 2017/6/5.
//  Copyright © 2017年 anjohnlv. All rights reserved.
//

#import "ViewController.h"
#import "LJCCollectionViewCell.h"
#import "BJCCollectionViewFlowLayout.h"

#define ScreenW [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ViewController
static NSString *ID=@"CELL";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // UICollectionView使用注意点
    // 1.创建UICollectionView必须要有布局参数
    // 2.cell必须通过注册
    // 3.cell必须自定义,系统cell没有任何子控件
    
    BJCCollectionViewFlowLayout *layout = [[BJCCollectionViewFlowLayout alloc] init];
    
//    layout.itemSize = CGSizeMake(60, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat margin = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
    layout.headerReferenceSize=CGSizeMake(300, 40);
    layout.footerReferenceSize=CGSizeMake(300, 40);
    // 设置最小行间距
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *collectView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectView.bounds=self.view.bounds;
    collectView.backgroundColor=[UIColor grayColor];
    collectView.center = self.view.center;
    collectView.dataSource=self;
    collectView.delegate=self;
    collectView.contentOffset=CGPointMake(0, 1000);
    
    [self.view addSubview:collectView];
    
    [collectView registerNib:[UINib nibWithNibName:NSStringFromClass([LJCCollectionViewCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:ID];
    //    [collectView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ReusableView"];
    [collectView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader"];
    [collectView registerClass:[UICollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"SectionFooter"];
    //ReusableView 这个名字需要和下面4中cellForItemAtIndexPath 定义的一样
}
- (CGFloat)fixSlitWith:(CGRect)rect colCount:(CGFloat)colCount space:(CGFloat)space {
    CGFloat totalSpace = (colCount - 1) * space;//总共留出的距离
    CGFloat itemWidth = (rect.size.width - totalSpace) / colCount;// 按照真实屏幕算出的cell宽度 （iPhone6 375*667）93.75
    CGFloat fixValue = 1 / [UIScreen mainScreen].scale; //(1px=0.5pt,6Plus为3px=1pt)
    CGFloat realItemWidth = floor(itemWidth) + fixValue;//取整加fixValue  floor:如果参数是小数，则求最大的整数但不大于本身.
    if (realItemWidth < itemWidth) {// 有可能原cell宽度小数点后一位大于0.5
        realItemWidth += fixValue;
    }
    CGFloat realWidth = colCount * realItemWidth + totalSpace;//算出屏幕等分后满足1px=([UIScreen mainScreen].scale)pt实际的宽度,可能会超出屏幕,需要调整一下frame
    CGFloat pointX = (realWidth - rect.size.width) / 2; //偏移距离
    rect.origin.x = -pointX;//向左偏移
    rect.size.width = realWidth;
//    _rect = rect;
    return realItemWidth; //每个cell的真实宽度
}
// 分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 5;
}
// 每一分区的单元个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 15;
}
//#pragma mark - X间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
//
//    return  0.001;
//}
//#pragma mark - Y间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
//
//    return  0;
//}
//footer的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{

    return CGSizeMake(300, 80);
}

//header的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(300, 100);
}
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
    return YES;
}
// 和UITableView类似，UICollectionView也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader                                                                           withReuseIdentifier:@"SectionHeader"                                                                              forIndexPath:indexPath];
        UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        contentView.backgroundColor = [UIColor yellowColor];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width, 30)];
        label.text = [NSString stringWithFormat:@"Section Header: %ld", indexPath.section];
        label.textAlignment = NSTextAlignmentCenter;
        [contentView addSubview:label];
        [headerView addSubview:contentView];
        
        return headerView;
    }
    else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
        UICollectionReusableView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter                                                                           withReuseIdentifier:@"SectionFooter"                                                                              forIndexPath:indexPath];
        UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        contentView.backgroundColor = [UIColor redColor];
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, self.view.frame.size.width, 30)];
        label.text = [NSString stringWithFormat:@"Section Footer    : %ld", indexPath.section];
        label.textAlignment = NSTextAlignmentCenter;
        [contentView addSubview:label];
        [headerView addSubview:contentView];
        
        return headerView;
    }
    
    return nil;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LJCCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.image=[UIImage imageNamed:@"AppIcon20x20@2x"];
    return cell;
}
- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout
{
    return nil;
}
///每个分区的外边距 上左下右
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

return UIEdgeInsetsMake(0, 0, 0, 0);

}

////每个单元的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

//    return CGSizeZero;
    if (indexPath.section==4) {
        return CGSizeMake(320, 50);
    }
   CGFloat w =  [self fixSlitWith:CGRectMake(0, 0, 320, 100) colCount:6 space:0];
//    CGFloat w =60;
    return CGSizeMake(w, w);

}



@end
